#include "Ball.h"
#include <QPainter>
#include <QDebug>
#include <QGraphicsSceneDragDropEvent>

Ball::Ball(QGraphicsObject *parent) :
	QGraphicsObject(parent), size(QSize(32, 32))
{
	setFlag(QGraphicsItem::ItemIsMovable, true);
}

void Ball::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsObject::mousePressEvent(event);
	setZValue(1);
	initialPosition = pos();
}

void Ball::mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsObject::mouseReleaseEvent(event);
	setZValue(0);
	QPointF newPosition = pos();
	emit moved(initialPosition, newPosition, this);
}

void Ball::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	painter->drawPixmap(boundingRect(), QPixmap(":/icons/ball4.png"), QRectF(0, 0, 32, 32));
}

QRectF Ball::boundingRect() const
{
	return QRectF(-size.width() / 2, -size.height() / 2, size.width(), size.height());
}

QPainterPath Ball::shape() const
{
	QPainterPath path;
	path.addEllipse(boundingRect());
	return path;
}

QSize Ball::getSize()
{
	return size;
}

void Ball::setSize(QSize _size)
{
	this->size = _size;
	update(boundingRect());
}
